<?php
namespace  controllers;
use Model\notice;
use Model\suppliers;

class notices extends api{
    public function notice(){
        $supplier_model = new suppliers();
        $supplier_info = $supplier_model->get_sup_info();
        $notice_model = new notice();
        $notice_list = $notice_model->find(array('receive'=>$supplier_info['supplier_name'],'supplier_id'=>$supplier_info['_id']));
        $notice_arr = array();
        foreach ($notice_list as $v){
            $notice_arr[date('Y-m',$v['create_time'])][] = $v;
        }
        $notice_arr1 = array();
        $notice_arr3 = array();
        $notice_arr2 = array();
        foreach ($notice_arr as $k=>$v){
            $notice_arr1['date'] = $k;
            $notice_arr1['num'] = count($v);
            $notice_arr1['items'] = array();
            foreach ($v as $vv){
                $notice_arr2['text'] = $vv['describe'];
                $notice_arr2['id'] = $vv['_id'];
                $notice_arr2['time'] = date('Y-m-d H:i',$vv['create_time']);
                $notice_arr2['type'] = $vv['is_see'];
                $notice_arr1['items'][] = $notice_arr2;
            }
            $notice_arr3[] = $notice_arr1;
        }

        $this->success($notice_arr3);
    }
    //获取消息
    public function get_history_notice(){
        $supplier_model = new suppliers();
        $supplier_info = $supplier_model->get_sup_info();

        $notice_model = new notice();
        $notice_list = $notice_model->find(array('create_name'=>$supplier_info['supplier_name'],'supplier_id'=>$supplier_info['_id'],'receive'=>'admin'));
        $notice_arr = array();
        foreach ($notice_list as $v){
            if(isset($v['sku_id'])){
                $notice_arr[date('Y-m',$v['create_time'])][] = $v;
            }
        }
        $notice_arr1 = array();
        $notice_arr3 = array();
        $notice_arr2 = array();
        foreach ($notice_arr as $k=>$v){
            $notice_arr1['date'] = $k;
            $notice_arr1['num'] = count($v);
            $notice_arr1['items'] = array();
            foreach ($v as $vv){
                $notice_arr2['text'] = $vv['describe'];
                $notice_arr2['time'] = date('Y-m-d',$vv['create_time']);
                $notice_arr1['items'][] = $notice_arr2;
            }
            $notice_arr3[] = $notice_arr1;
        }
        $this->success($notice_arr3);
    }
    //标记为已读
    public function is_see(){
        $id = $_REQUEST['id']['$id'];
        $notice_model = new notice();
        $res = $notice_model->update(array('_id'=>new \MongoId($id)),array('$set'=>array('is_see'=>1)));
        if($res['updatedExisting']){
            $this->notice();
        }else{
            $this->error('失败');
        }
    }


}